package com.gryffonx.NameGenerators;

import com.gryffonx.DiceRoller.StaticDiceRoller;
import com.gryffonx.utils.StringUtils;
import com.gryffonx.utils.XMLUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.File;

/**
 * @author Erik Stell
 * @version 1.0
 */
public class TitleGenerator {
    private static final File fileToBeParsed = new File("src/com/gryffonx/xml/titles.xml");
    private static final SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;


    public static String generateName(int howMany) throws Exception {
        StringBuffer retVal = new StringBuffer();

        if (howMany <= 0) {
            howMany = 1;
        }

        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //determine the pattern that will be used.
        int whatPattern = determinePattern();

        for (int i = 0; i < howMany; i++) {
            StringBuffer adjVal = new StringBuffer();

            if (whatPattern == 1) {
                adjVal.append(generateGroup1());
            } else if (whatPattern == 2) {
                adjVal.append(generateGroup2());
            } else if (whatPattern == 3) {
                adjVal.append(generateGroup3());
            }
            //assign the retVal to the name generated
            retVal.append(adjVal);
        }
        return retVal.toString();
    }

    private static String generateGroup1() {
        StringBuffer retVal = new StringBuffer();
        //PREFIX1+PREFIX3
        try {
            retVal.append(" \"The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
            retVal.append("\"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal.toString();
    }

    private static String generateGroup2() {
        StringBuffer retVal = new StringBuffer();
        //PREFIX2+PREFIX3
        try {
            retVal.append(" \"The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 3)));
            retVal.append(XMLUtils.getNodeValue("prefix", xmlDoc, 2));
            retVal.append("\"");            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal.toString();
    }

    private static String generateGroup3() {
        StringBuffer retVal = new StringBuffer();
        //PREFIX3+PREFIX3
        try {
            retVal.append(" \"");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 3)));
            retVal.append(XMLUtils.getNodeValue("prefix", xmlDoc, 3));
            retVal.append("\"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal.toString();
    }

    private static int determinePattern() throws Exception {
        int rollVal = StaticDiceRoller.rolldSomething(3, 1, true);
        //System.out.println("Title Generator Pattern: " + rollVal);
        return rollVal;
    }
}
